package com.xique.blelock.Utils;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.widget.Toast;




public class ToastUtil {
    private static ToastUtil utils;


    public static ToastUtil getInstance() {
        if (utils == null) {
            synchronized (ToastUtil.class) {
                if (utils == null) {
                    utils = new ToastUtil();
                }
            }
        }
        return utils;
    }

    /**
     * 弹提示框
     *
     * @param context
     * @param content 提示信息
     */
    public void showInforDialog(Context context, String content) {
        Builder builder = new Builder(context);
        builder.setPositiveButton("确	认", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setTitle("提示信息");
        builder.setMessage(content);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    private Toast mToast;
    private Handler mHandler = new Handler();
    private Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };

    /**
     * 吐司
     *
     * @param text
     */
    public void showToast(Context context, String text) {

        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(text);
        else
            mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
        mHandler.postDelayed(r, 2000);

        mToast.show();
    }

    /**
     * 吐司
     *
     * @param resId
     */
    public void showToast(Context context, int resId) {
        showToast(context, context.getResources().getString(resId));
    }

}